import {Component, OnInit} from '@angular/core';
import {LoadingService} from '../../../shared/loading.service';
import {WithdrawalsService} from '../withdrawals.service';
import {AlertService} from '../../../shared/alert.service';
import {ActivatedRoute} from '@angular/router';

@Component({
    selector: 'app-withdrawals-view',
    templateUrl: './withdrawals-view.component.html',
    styleUrls: ['./withdrawals-view.component.scss']
})
export class WithdrawalsViewComponent implements OnInit {

    public dataArr: any = [];
    public id: any;
    public state: any = '';
    public states: any;
    public showType: any = '';

    constructor(public loading: LoadingService,
                public service: WithdrawalsService,
                public alert: AlertService,
                public router: ActivatedRoute) {
    }

    ngOnInit() {
        this.router.params.subscribe(params => {
            this.id = params.id;
            // this.showType = params.state;
        });
        this.getData();
    }

    getData() {
        this.loading.show();
        const params: any = {
            id: this.id,
            pay_type: 2
        }
        this.service.auditDetail(params).subscribe(
            res => {
                this.loading.hide();
                console.log(res, '请求的数据');
                if (res['code'] == 0) {
                    this.dataArr = res['data'];
                    this.showType = res['data']['pay_state']
                    this.dataArr['add_time'] = this.formatDateTime(this.dataArr['add_time'].substring(0, 10) * 1000);

                } else {
                    this.alert.show(res['message']);
                }
            }, error => {
                this.loading.hide();
                this.alert.show(error['message']);
            }
        );
    }


    formatDateTime(inputTime) {
        const time = new Date(parseInt(inputTime));
        const y = time.getFullYear();
        const m = time.getMonth() + 1;
        const min = m < 10 ? ('0' + m) : m;
        const d = time.getDate();
        const day = d < 10 ? ('0' + d) : d;
        const h = time.getHours();
        const hours = h < 10 ? ('0' + h) : h;
        const minute = time.getMinutes();
        const second = time.getSeconds();
        const minutes = minute < 10 ? ('0' + minute) : minute;
        const seconds = second < 10 ? ('0' + second) : second;
        // return y + '.' + min + '.' + day;
        return y + '-' + min + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    };


    back() {
        window.history.go(-1);
    }


    // 选择
    changes(i) {
        // (0, '准备中'),(1, '提现失败'),(2, '提现中'),(3, '提现成功')
        if (i === 1) {
            this.states = 1;
        }
        if (i === 3) {
            this.states = 3;
        }
    }

    submit() {
        if (this.states == '') {
            this.alert.show('请选择打款状态');
            return;
        }
        this.loading.show();
        const params: any = {
            id: this.id,
            pay_state: this.states
        }
        console.log(params, '审核');
        this.service.auditwithdrawal(params).subscribe(
            res => {
                if (res['code'] == 0) {
                    this.alert.show('审核成功');
                    setTimeout(() => {
                        window.history.go(-1);
                    }, 450);
                } else {
                    this.alert.show(res['message']);
                }
                this.loading.hide();
            }, error => {
                this.loading.hide();
                this.alert.show(error['message']);
            }
        );
    }

}
